package com.xywy.tangandroid.activity;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONException;
import org.json.JSONObject;

import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.VolleyError;
import com.google.gson.reflect.TypeToken;
import com.xywy.tangandroid.R;
import com.xywy.tangandroid.adapter.OnLineMessageAdapter;
import com.xywy.tangandroid.base.BaseActivity;
import com.xywy.tangandroid.bean.OnlineQuestionList;
import com.xywy.tangandroid.bean.User;
import com.xywy.tangandroid.util.Constant;
import com.xywy.tangandroid.util.JsonUtil;
import com.xywy.tangandroid.util.UIUtil;
import com.xywy.tangandroid.vollery.GetRequest;
import com.xywy.tangandroid.vollery.OnVolleyResponseListener;

/**
 * 消息盒子，在线提问；
 */
public class OnlineMessageAct extends BaseActivity {

	private ListView message_listview;
	private List<OnlineQuestionList> list = new ArrayList<OnlineQuestionList>();
	private OnLineMessageAdapter adapter;
	private RelativeLayout left_img;
	private TextView middle_title;
	private String Tag = "OnlineMessageAct";

	@Override
	protected void initView(Bundle savedInstanceState) {
		setContentView(R.layout.act_online_messages);
		left_img = (RelativeLayout) findViewById(R.id.left_img);
		middle_title = (TextView) findViewById(R.id.middle_title);
		middle_title.setText(getResources().getString(
				R.string.online_ask_message));
		message_listview = (ListView) findViewById(R.id.message_listview);
	}

	@Override
	protected void initListener() {
		left_img.setOnClickListener(this);
	}

	private final int page_size = 20;
	private final int page_number = 1;

	/**
	 * 获取数据方法
	 */
	@Override
	protected void initData() {
		getQuestOnline(page_size, page_number);
	}

	@Override
	public void onClick(View v) {
		super.onClick(v);
		switch (v.getId()) {
		case R.id.left_img:
			finish();
			break;

		default:
			break;
		}
	}

	Handler mHandler = new Handler() {
	};

	/**
	 * 获取在线提问列表信息
	 * 
	 * @param psize
	 * @param pagenume
	 */

	private void getQuestOnline(int psize, int pagenume) {
		GetRequest<String> getRequest = new GetRequest<String>(Constant.getUrl(
				"club", "Club_QuesList")
				+ "&userid="
				+ User.getIUserInstance(context).getUserid()
				+ "&fromurl="
				+ "app_tang" + "&psize=" + psize + "&pagenum=" + pagenume,
				String.class, new OnVolleyResponseListener<String>() {

					@Override
					public void onError(VolleyError volleyError) {
						UIUtil.DissDialog(context, waittingDialog);
						UIUtil.showToast(context,
								getResources().getString(R.string.network_slow));
					}

					@Override
					public void onSuccess(String response) {
						UIUtil.DissDialog(context, waittingDialog);
						try {
							JSONObject jsonObject = new JSONObject(response);
							int state = jsonObject.getInt("state");
							if (state == 200) {
								if (jsonObject.has("data")
										&& jsonObject.getString("data")
												.length() > 0) {
									if (jsonObject.getString("data").length() == 0) {
										Toast.makeText(context, "暂无在线提问信息",
												Toast.LENGTH_SHORT).show();
									}
									list.addAll((List<OnlineQuestionList>) JsonUtil.parseJsonToList(
											jsonObject.getString("data"),
											new TypeToken<List<OnlineQuestionList>>() {
											}.getType()));
									adapter = new OnLineMessageAdapter(
											OnlineMessageAct.this, list);
									message_listview.setAdapter(adapter);
								}
							} else {
								// TODO: 此处展示无数据位置
								UIUtil.showToast(context,
										jsonObject.getString("msg"));
							}
						} catch (JSONException e) {
							e.printStackTrace();
						}

					}
				});
		getRequest.setIsParseJson(false); // 如果使用String 不会自动解析json
		executeRequest(getRequest);
	}
}
